﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UTC_Project_Manager.Model.Infrastructure;
using System.ComponentModel;
using DlhSoft.Windows.Data;

namespace UTC_Project_Manager.Controls
{
    /// <summary>
    /// Interaction logic for MemberSettingsControl.xaml
    /// </summary>
    public partial class MemberSettingsControl : UserControl
    {
        private bool isInit;

        public MemberSettingsControl()
        {
            InitializeComponent();
            this.memberListView.ItemsSource = App.MyConfig.OurProject.Members;
            disableButton();
            disableEdit();
            isInit = false;

        }

        private delegate void focus(TextBox addTextBox);

        private void memberListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (this.memberListView.SelectedIndex != -1)
            {
                enableEdit();
                isInit = true;
                Member currentMember = this.memberListView.SelectedItem as Member;
                this.firstNameTextBox.Text = currentMember.FirstName;
                this.nameTextBox.Text = currentMember.LastName;
                this.telTextBox.Text = currentMember.Phone;
                this.emailTextBox.Text = currentMember.Email;
                isInit = false;
                this.removeNoteButton.IsEnabled = true;
            }
            else
                this.removeNoteButton.IsEnabled = false;

        }

        private void addNoteButton_Click(object sender, RoutedEventArgs e)
        {

            enableEdit();
            isInit = true;
            this.firstNameTextBox.Clear();
            this.nameTextBox.Clear();
            this.telTextBox.Clear();
            this.emailTextBox.Clear();
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.SystemIdle,
                new focus(setTextBoxFocus), this.nameTextBox);
            this.memberListView.SelectedIndex = -1;
            isInit = false;
            this.cancelButton.IsEnabled = true;
        }

        // Set the focus to the current TextBox
        private void setTextBoxFocus(TextBox addTextBox)
        {
            addTextBox.Focus();
        }

        private void removeNoteButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.memberListView.SelectedIndex != -1)
            {
                App.MyConfig.OurProject.Members.Remove(this.memberListView.SelectedItem as Member);
                App.MyConfig.assignableResourceItems.RemoveAt(this.memberListView.SelectedIndex);
                this.firstNameTextBox.Clear();
                this.nameTextBox.Clear();
                this.telTextBox.Clear();
                this.emailTextBox.Clear();
                disableEdit();
                disableButton();
                ICollectionView view = CollectionViewSource.GetDefaultView(this.memberListView.ItemsSource);
                view.Refresh();

            }
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            disableButton();
            if (this.memberListView.SelectedIndex == -1)
            {
                Member member = new Member(this.nameTextBox.Text, this.firstNameTextBox.Text, this.telTextBox.Text, this.emailTextBox.Text);
                App.MyConfig.OurProject.Members.Add(member);
                App.MyConfig.assignableResourceItems.Add(new ResourceItem {Content = member.ToString()});
                this.firstNameTextBox.Clear();
                this.nameTextBox.Clear();
                this.telTextBox.Clear();
                this.emailTextBox.Clear();
                disableEdit();
                disableButton();

            }
            else
            {
                Member currentMember = this.memberListView.SelectedItem as Member;
                currentMember.FirstName = this.firstNameTextBox.Text;
                currentMember.LastName = this.nameTextBox.Text;
                currentMember.Phone = this.telTextBox.Text;
                currentMember.Email = this.emailTextBox.Text;
                

            }
            ICollectionView view = CollectionViewSource.GetDefaultView(this.memberListView.ItemsSource);
            view.Refresh();

        }

        private void disableButton()
        {
            this.okButton.IsEnabled = false;
            this.cancelButton.IsEnabled = false;
        }

        private void enableButton()
        {
            this.okButton.IsEnabled = true;
            this.cancelButton.IsEnabled = true;
        }

        private void nameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (isInit == false)
                enableButton();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            disableButton();
            if (this.memberListView.SelectedIndex != -1)
            {
                Member currentMember = this.memberListView.SelectedItem as Member;
                this.firstNameTextBox.Text = currentMember.FirstName;
                this.nameTextBox.Text = currentMember.LastName;
                this.telTextBox.Text = currentMember.Phone;
                this.emailTextBox.Text = currentMember.Email;
            }
            else
            {
                this.firstNameTextBox.Clear();
                this.nameTextBox.Clear();
                this.telTextBox.Clear();
                this.emailTextBox.Clear();
                disableEdit();
            }

        }

        private void disableEdit()
        {
            this.telTextBox.IsEnabled = false;
            this.firstNameTextBox.IsEnabled = false;
            this.nameTextBox.IsEnabled = false;
            this.emailTextBox.IsEnabled = false;

        }

        private void enableEdit()
        {
            this.telTextBox.IsEnabled = true;
            this.firstNameTextBox.IsEnabled = true;
            this.nameTextBox.IsEnabled = true;
            this.emailTextBox.IsEnabled = true;

        }




    }
}

